﻿using System;
using System.Collections.Generic;
using System.Linq;
using Stamp_For_Africa_Version_2.Models;

namespace Stamp_For_Africa_Version_2.Repositories
{
	public class AlbumRepository
	{
		private StampsDataContext db = new StampsDataContext();
		//
		// Query Methods

		public IQueryable<Album> FindAllAlbums()
		{
			return db.Albums;
		}

		public IQueryable<Album> FindAllAlbums(Guid userId)
		{
			return db.Albums.Where(u => u.UserId == userId);
		}

		public Album GetAlbum(Guid id)
		{
      Album album = db.Albums.SingleOrDefault(d => d.Id == id);
      //if (album != null)
//		    album.Stamps = GetAllStampsInAlbum(album);
		  return album;
		}
		
		//
		// Insert/Delete Methods

		public void Add(Album album)
		{
			db.Albums.InsertOnSubmit(album);
		}

		public void Delete(Album album)
		{
			db.Albums.DeleteOnSubmit(album);
		}

    public AlbumEntry GetAlbumEntryForStamp(Guid albumId, Guid stampId, int pageNumber)
    {
     return  db.AlbumEntries.Where(u => u.AlbumId == albumId && u.StampId == stampId && u.Page == pageNumber).FirstOrDefault();
    }

    public int GetRemainingAssignableQuantityForStamp(Guid userId, Guid stampId, Collection.StampState stampState)
    {
      var albumEntries = db.AlbumEntries.Where(u => u.StampId == stampId);
      var collectionEntries = db.Collections.Where(u => u.UserId == userId && u.StampId == stampId && u.State == stampState).SingleOrDefault();

      if (collectionEntries != null)
      {
        return collectionEntries.Quantity - albumEntries.Count();
      }
      return 0;

    }

    public void SaveOrderOfStampsOnPage(Guid albumId, int pageNumber, List<Guid> orderedStampList )
    {
      int i = 0;
      foreach (var guid in orderedStampList)
      {
        AlbumEntry entry = GetAlbumEntryForStamp(albumId, guid, pageNumber);
        if (entry != null)
        {
          entry.index = i;
        }
        i++;
      }

      Save();
    }

    public IQueryable<Stamp> GetAllStampsInAlbum(Album album)
    {
      //For now lets just return the stamps
      var stampGuids = db.AlbumEntries.Where(u => u.AlbumId == album.Id).Select(u => u.StampId);
      return db.Stamps.Where(u => stampGuids.Contains(u.Id));
    }

		public IQueryable<Stamp> GetAllStampsInAlbumFromPage(Album album, int pageNumber)
		{
		  return  db.AlbumEntries.Where(u => u.AlbumId == album.Id && u.Page == pageNumber).OrderBy(u => u.index).Select(u => u.Stamp);
			//return db.Stamps.Where(u => stampGuids.Contains(u.Id));			
		}

    public void AddStampToAlbum(Guid albumId, Guid stampId, int pageNumber)
    {
      AlbumEntry albumEntry = new AlbumEntry();
      
      
      albumEntry.StampId = stampId;
      albumEntry.AlbumId = albumId;
      albumEntry.Id = Guid.NewGuid();
      albumEntry.Col = 0;
      albumEntry.Row = 0;
      albumEntry.Page = pageNumber;

      db.AlbumEntries.InsertOnSubmit(albumEntry);
      Save();
    }

    public void RemoveStampFromAlbum(Guid albumId, Guid stampId)
    {
      AlbumEntry entry = db.AlbumEntries.Where(u => u.AlbumId == albumId && u.StampId == stampId).FirstOrDefault();
      if (entry != null)
      {
        db.AlbumEntries.DeleteOnSubmit(entry);
        Save();
      }
    }

    public void AddPage(Guid id)
    {
      Album album = GetAlbum(id);
      album.PageCount++;
      Save();
    }

		//
		// Persistence

		public void Save()
		{
			db.SubmitChanges();
		}
	}
}